package com.example.statistics.weight

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RadioButton
import android.widget.RadioGroup

class ToggleableRadioGroup @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RadioGroup(context, attrs) {

    private var lastCheckedId = View.NO_ID

    init {

        setOnClickListener {
            val checkedRadioButtonId = checkedRadioButtonId
            if (checkedRadioButtonId != -1) {
                val radioButton = findViewById<RadioButton>(checkedRadioButtonId)
                if (radioButton.isChecked) {
                    // 如果已经选中，则取消选中
                    clearCheck()
                } else {
                    // 如果未选中，则选中
                    radioButton.isChecked = true
                }
            }
        }
    }
}